<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OrderController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $status=$request->input('status');
        $keyword=$request->input('keyword');
        $order_type=$request->input('order_type');
        $data=Order::with('detail.products')->when($status,function($query)use($status){
            if(in_array($status,[10,20,30,40,50])){
                $query->where('status',$status);
            }
            if($status==2){
                $query->where('order_type',2)->orderBy('status','desc');
            }
        })->when($keyword,function($query) use($keyword){
            $query->where('order_no',$keyword);
        })->when($order_type,function ($query) use ($order_type){
            $query->where('order_type',3);
        })->orderBy('created_at','desc')->paginate(10);

//        $data= Admins::when($keyword,function($query) use($keyword){
//            $query->where('username','like',"%$keyword%")->get();
//        })->with(['role'])->paginate($request->input('limit',10));
       return  $this->success($data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
      $res= DB::table('order')->whereId($id)->first();

     if($res->pay_mode==1){
         $pay='PayPal';
     }elseif ($res->pay_mode==2){
         $pay='錢包';
     }else{
         $pay='線下';
     }

      return $this->success( $this->pdf($res->order_no,$res->pay_price,date('Y-m-d H:i',$res->created_at),$pay));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $status=$request->input('status');
        DB::table('order')->where('id',$id)->update(['status'=>$status]);
        return $this->success();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function pdf($order,$price,$time,$pay)
    {
        $pdf = new \TCPDF();
        // 设置文档信息
        $pdf->SetCreator('電子發票');
        $pdf->SetAuthor('電子發票');
        $pdf->SetTitle('電子發票');
        $pdf->SetSubject('店址發票');
        $pdf->SetKeywords('電子, PDF, PHP');

        // 设置页眉和页脚信息
        $pdf->SetHeaderData('tcpdf_logo.jpg', 30, 'LanRenKaiFA.com', '電子發票', [0, 64, 255], [0, 64, 128]);
        $pdf->setFooterData([0, 64, 0], [0, 64, 128]);

        // 设置页眉和页脚字体
        $pdf->setHeaderFont(['stsongstdlight', '', '10']);
        $pdf->setFooterFont(['helvetica', '', '8']);

        // 设置默认等宽字体
        $pdf->SetDefaultMonospacedFont('courier');

        // 设置间距
        $pdf->SetMargins(15, 15, 15);//页面间隔
        $pdf->SetHeaderMargin(5);//页眉top间隔
        $pdf->SetFooterMargin(10);//页脚bottom间隔

        // 设置分页
        $pdf->SetAutoPageBreak(true, 25);

        // set default font subsetting mode
        $pdf->setFontSubsetting(true);

        //设置字体 stsongstdlight支持中文
        $pdf->SetFont('stsongstdlight', '', 14);

        //第一页
        $pdf->AddPage();
        $pdf->writeHTML("<div style='text-align: center'><h1>訂單號：$order</h1></div>");
        $pdf->writeHTML("<p>價格：$price</p>");
//        $pdf->writeHTML('<p style="color: red">我是第二行内容</p>');
        $pdf->writeHTML("<p>下單時間：$time</p>");
        $pdf->writeHTML("<p>支付方式：$pay</p>");
        $pdf->Ln(5);//换行符
//        $pdf->writeHTML('<p><a href="http://www.lanrenkaifa.com/" title="">懒人开发网</a></p>');

        //第二页
//        $pdf->AddPage();
//        $pdf->writeHTML('<h1>第二页内容</h1>');

        //输出PDF
//      $s= $pdf->Output('t.pdf', 'E:/phpstudy_pro/WWW/laravel-admin-api/storage');//I输出、D下载
//        return $this->success($s);
//        echo __DIR__;
        $name= storage_path(). '/app/public/pdf/';

        $fileName= date('YmdHis') . '.pdf';
        $s= $pdf->Output($name.$fileName, 'F');

        return $fileName;
    }


}
